package gui;

import gui.component.ContactChooser;
import java.awt.FlowLayout;
import javax.swing.JOptionPane;

/**
 *
 * @author Tom
 */
public class UINewMail extends javax.swing.JDialog 
{
    UIMain parent; 
    ContactChooser cchooserA;
    ContactChooser cchooserCC;
    ContactChooser cchooserBCC;
    
    public UINewMail(UIMain parent) 
    {
        super(parent,true);
        this.parent=parent;
        initComponents();       
                     
        pnTo.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        pnCC.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        pnBCC.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        
        cchooserA=new ContactChooser(parent.user);
        cchooserCC = new ContactChooser(parent.user);
        cchooserBCC = new ContactChooser(parent.user);
        
        pnTo.add(cchooserA);
        pnCC.add(cchooserCC);
        pnBCC.add(cchooserBCC);
        
        pnCCBCC.setVisible(false);
        setLocationRelativeTo(null);        
    }
    
    public UINewMail(UIMain parent, String subject, String content, String TO, String CC, String BCC)
    {        
        this(parent);
        if (TO!=null)
        {
            String[] TOs = TO.split(",");
            for (String t : TOs) 
            {
                cchooserA.addContact(t);
            }
        }
        if (CC!=null)
        {
            String[] CCs = CC.split(",");
            for (String cc : CCs) 
            {
                cchooserCC.addContact(cc);
            }
        }
        if (BCC!=null)
        {
            String[] BCCs = BCC.split(",");
            for (String bcc : BCCs) 
            {
                cchooserBCC.addContact(bcc);
            }
        }
        if (subject!=null)
            txtSubject.setText(subject);
        if (content!=null)
            txtMessage.setText(content);
        txtMessage.requestFocus();
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtSubject = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtMessage = new javax.swing.JTextArea();
        btSend = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();
        btAdd = new javax.swing.JButton();
        pnCCBCC = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        pnCC = new javax.swing.JPanel();
        pnBCC = new javax.swing.JPanel();
        chbEncrypt = new javax.swing.JCheckBox();
        chbSign = new javax.swing.JCheckBox();
        pnTo = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Objet");

        jLabel2.setText("A");

        txtMessage.setColumns(20);
        txtMessage.setRows(5);
        jScrollPane1.setViewportView(txtMessage);

        btSend.setText("Envoyer");
        btSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSendActionPerformed(evt);
            }
        });

        btCancel.setText("Annuler");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });

        btAdd.setText("+");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        jLabel3.setText("CC");

        jLabel4.setText("BCC");

        pnCC.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        pnBCC.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout pnCCBCCLayout = new javax.swing.GroupLayout(pnCCBCC);
        pnCCBCC.setLayout(pnCCBCCLayout);
        pnCCBCCLayout.setHorizontalGroup(
            pnCCBCCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnCCBCCLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnCCBCCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnCCBCCLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(10, 10, 10)
                        .addComponent(pnBCC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnCCBCCLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(16, 16, 16)
                        .addComponent(pnCC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnCCBCCLayout.setVerticalGroup(
            pnCCBCCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnCCBCCLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnCCBCCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnCCBCCLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnCC, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnCCBCCLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnBCC, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44))
        );

        chbEncrypt.setText("Chiffrer le message");
        chbEncrypt.setEnabled(false);
        chbEncrypt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbEncryptActionPerformed(evt);
            }
        });

        chbSign.setText("Signer le message");
        chbSign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbSignActionPerformed(evt);
            }
        });

        pnTo.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnTo.setAlignmentX(0.0F);
        pnTo.setAlignmentY(0.0F);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(pnTo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtSubject)))
                    .addComponent(pnCCBCC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(chbSign)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(chbEncrypt)
                                .addGap(0, 522, Short.MAX_VALUE))
                            .addComponent(jScrollPane1)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(btSend, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnTo, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnCCBCC, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chbSign)
                    .addComponent(chbEncrypt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btSend, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelActionPerformed

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        pnCCBCC.setVisible(!pnCCBCC.isVisible());
    }//GEN-LAST:event_btAddActionPerformed

    private void btSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSendActionPerformed
        String emailA = cchooserA.getAllEmails();
        String emailCC = cchooserCC.getAllEmails();
        String emailBCC = cchooserBCC.getAllEmails();
        if (emailA==null && emailCC==null && emailBCC==null)
        {
            JOptionPane.showMessageDialog(this, "Sélectionez au moins un destinataire!", "", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            if (chbEncrypt.isSelected() && (!cchooserA.isEveryoneHasKey() || !cchooserCC.isEveryoneHasKey() || !cchooserBCC.isEveryoneHasKey()))
            {
                JOptionPane.showMessageDialog(this, "Pour envoyer un message chiffré, tous les destinatiares doivent avoir leurs clés publiques", "", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {               
                parent.user.databaseWorker.sendMail(txtSubject.getText()==null? "<Sans objet>":txtSubject.getText(), txtMessage.getText()==null? "<Message vide>":txtMessage.getText(), emailA, emailCC, emailBCC, chbSign.isSelected(), chbEncrypt.isSelected());
                this.dispose();
            }
        }
    }//GEN-LAST:event_btSendActionPerformed

    private void chbSignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbSignActionPerformed
        chbEncrypt.setEnabled(chbSign.isSelected());
        if (!chbSign.isSelected())
        {
            chbEncrypt.setSelected(false);
        }
    }//GEN-LAST:event_chbSignActionPerformed

    private void chbEncryptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbEncryptActionPerformed

    }//GEN-LAST:event_chbEncryptActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btSend;
    private javax.swing.JCheckBox chbEncrypt;
    private javax.swing.JCheckBox chbSign;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnBCC;
    private javax.swing.JPanel pnCC;
    private javax.swing.JPanel pnCCBCC;
    private javax.swing.JPanel pnTo;
    private javax.swing.JTextArea txtMessage;
    private javax.swing.JTextField txtSubject;
    // End of variables declaration//GEN-END:variables
}
